package com.example.base.demo.controller;

import com.example.base.demo.condition.TestCondition;
import com.example.base.demo.configuration.TestProperties;
import com.example.base.demo.i18n.Result;
import com.example.base.demo.json.User;
import com.example.base.demo.pojo.Animal;
import com.example.base.demo.pojo.Db;
import com.example.base.demo.sp.TestEvent;
import com.example.base.test.TestScanProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;

import java.io.Console;
import java.io.IOException;
import java.util.*;


@RestController
@RequestMapping("/study")
public class StudyController {
    private static final Logger log = LoggerFactory.getLogger(StudyController.class);
    @Value("#{systemProperties['os.name']}")
    private String osName; // 注入操作系统属性=》Windows 10

    @Value("#{ 5*16 }")
    private int randomNumber; //注入表达式结果=》80

    @Value("#{animal.name}")
    private String animalName; // 注入名称为`animal`Bean 对象的属性值=》张三


    @Autowired
    Animal animal;

    @Autowired
    Environment environment;

    @Autowired
    Db Db;

    @GetMapping("/test")
    public Object test() {
        String property = environment.getProperty("person.name");
        return property;
    }

    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("/json")
    public User json() throws JsonProcessingException {
        // 序列化
        User user = new User("张大大", 2);
        String writeValueAsString = objectMapper.writeValueAsString(user);
        System.out.println(writeValueAsString);
        // 反序列化
        User readValue = objectMapper.readValue(writeValueAsString, User.class);
        return readValue;
    }

    @Value("${person.name}")
    String personName;

    @Autowired
    ApplicationArguments springApplicationArguments;

    @GetMapping("/args")
    public Object args() {
        // 获取原始的参数
        // ：
        String[] sourceArgs = springApplicationArguments.getSourceArgs();
        // 获取非选项参数
        List<String> nonOptionArgs = springApplicationArguments.getNonOptionArgs();
        // 获取参数的KEY
        Set<String> argsOptionNames = springApplicationArguments.getOptionNames();
        // 获取参数KEY对应的值，可以多个
        List<String> optionValues = springApplicationArguments.getOptionValues("name");
        // 是否包含该参数
        boolean age = springApplicationArguments.containsOption("age");
        System.out.println("springApplicationArguments");
        System.out.println(Arrays.toString(sourceArgs));
        System.out.println(nonOptionArgs);
        System.out.println(argsOptionNames);
        System.out.println(optionValues);
        return "Hello Boot";
    }


    @GetMapping("/publish")
    public Object publish() {
        // 发布事件
        TestEvent testEvent = new TestEvent(this, "测试消息");
        applicationContext.publishEvent(testEvent);
        return "Hello Boot";
    }

    @Autowired
    ConfigurableApplicationContext applicationContext;

    @RequestMapping("/shutdown")
    public String shutdown() {
        SpringApplication.exit(applicationContext, (ExitCodeGenerator) () -> 0);
        //System.exit( SpringApplication.exit(applicationContext,(ExitCodeGenerator)()->0));
        // 正常终止当前运行的Java虚拟机
        // 正常退出 是指如果当前程序还有在执行的任务，则等待所有任务执行完成以后再退出；非正常退出 是只要时间到了，
        // 立刻停止程序运行，不管是否还有任务在执行。这个效果就类似于 linux 里面的 kill -9 和 kill -15 。
        //System.exit(0);
        // 关闭应用
        //SpringApplication.exit(applicationContext,(ExitCodeGenerator)()->0);
        return "shutdown";
    }
    @Autowired
    TestCondition testCondition;


    @GetMapping("/yml")
    public String yml() throws IOException {
        // 1. 加载为Properties
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(new ClassPathResource("application.yml"));
        Properties properties = yamlPropertiesFactoryBean.getObject();

        // 2. 加载为Map
        YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean();
        yamlMapFactoryBean.setResources(new ClassPathResource("application.yml"));
        Map<String, Object> map = yamlMapFactoryBean.getObject();
        // 3. 加载为PropertySource
        YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
        Resource path = new ClassPathResource("application.yml");
        PropertySource<?> propertySource = loader.load("test-resource", path).get(0);
        return "Boot";
    }

    @RequestMapping(value = "/testI18n")
    public Result testI18n() {
        return Result.success();
    }

    @Autowired
    TestProperties testProperties;

    @Autowired
    TestScanProperties testScanProperties;
}
